﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RMS.Pages.Helpers;

namespace RMS.Models
{
    /* Class for managing various cart actions */

    public class Cart {
        
        private List<CartLine> lineCollection = new List<CartLine>();
        
        // Add Retreat to Cart
        public void AddItem(Retreat retreat, int quantity) {
            CartLine line = lineCollection
                .Where(p => p.Retreat.Ret_ID == retreat.Ret_ID)
                .FirstOrDefault();

            if (line == null) {
                lineCollection.Add(new CartLine {
                Retreat = retreat,
                Quantity = quantity });
            }   else {
                line.Quantity += quantity;
                }
        }

        public void RemoveLine(Retreat retreat) {
            lineCollection.RemoveAll(l => l.Retreat.Ret_ID == retreat.Ret_ID);
        }
              
        public decimal ComputeTotalValue()
        {
            return lineCollection.Sum(e => e.Retreat.Ret_Cost * e.Quantity);
        }

        public void Clear() {
            lineCollection.Clear();
        }

        public IEnumerable<CartLine> Lines {
            get { return lineCollection; }
        }
      
    }

    public class CartLine {
        public Retreat Retreat { get; set; }
        public int Quantity { get; set; }
    }
}